package com.example.battle.handler;

import com.example.battle.entity.sys.Matched;
import com.example.battle.repository.MatchedRespository;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
@Data
public class Match {
    @Autowired
    private MatchedRespository matchedRespository;
    //等待队列
    private Map<String, AccountMessage> map = new HashMap<>(); // <userID, user信息>
    //匹配成功列表
    private Map<String, String> mapMatched = new HashMap<>(); // <userID, opponentID>
    //等待好友
    private Map<String, AccountMessage> mapFriend = new HashMap<>();

    public void addUser(String userId, AccountMessage avarlurl){
        map.put(userId, avarlurl);
    }

    public void removeUser(String userId, String opponent){
        // 记录用户匹配成功信息，在t_matched表里。
        // 删除等待列表用户信息
        AccountMessage accountMessage1 = new AccountMessage();
        accountMessage1 = map.get(userId);
        AccountMessage accountMessage2 = new AccountMessage();
        accountMessage2 = map.get(opponent);
        Matched matched = new Matched();
        matched.setNickName1(accountMessage1.getNickName());
        matched.setNickName2(accountMessage2.getNickName());
        matched.setTime(new Date());
        matchedRespository.save(matched);
        map.remove(userId);
        map.remove(opponent);
    }
    public String findOpponment(String userID){
        for (Map.Entry<String, AccountMessage> entry : map.entrySet()) {
            // System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            if(!entry.getKey().equals(userID)){
                return entry.getKey();
            }
        }
        return null;
    }
    public String findFriend(String avatarUrl){
        for (Map.Entry<String, AccountMessage> entry : mapFriend.entrySet()) {
            // System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            if(!entry.getValue().getAvatarUrl().equals(avatarUrl)){
                return entry.getKey();
            }
        }
        return null;
    }

    public String findMatched(String userID){
        for (Map.Entry<String, String> entry : mapMatched.entrySet()) {
            // System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            if(entry.getKey().equals(userID)){
                return entry.getValue();
            }else if(entry.getValue().equals(userID)){
                return entry.getKey();
            }
        }
        return null;
    }

    public boolean removeMatched(String userId){
        for (Map.Entry<String, String> entry : mapMatched.entrySet()) {
            // System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            if(entry.getKey().equals(userId)){
                mapMatched.remove(entry);
                return true;
            }else if(entry.getValue().equals(userId)){
                mapMatched.remove(entry);
                return true;
            }
        }
        return false;
    }
    public void addMatched(String userId1, String userId2){
        mapMatched.put(userId1,userId2);
    }
}
